from typing import Optional
from fastapi import APIRouter, Depends, Query

from sqlalchemy.orm import Session

from app.tools_jumpserver.models.jumpserver_audit import JumpserverAudit
from common.database import get_db
from common.utils import is_empty
from core.response import resp_200

router = APIRouter()


@router.get('/audit', summary="堡垒机审计", description="工具-jumpserver审计")
async def get_jumpserver_audit(create_date: str, db: Session = Depends(get_db), page: int = Query(1, ge=1),
                               per_page: int = Query(10, ge=0), search: Optional[str] = None):
    if is_empty(search):
        data = JumpserverAudit.get_paginate(db, page, per_page, create_date)
    else:
        data = JumpserverAudit.get_search_paginate(db, page, per_page, create_date, search)

    return resp_200(data)


@router.get('/audit/export', summary="导出审计数据", description="工具-jumpserver审计")
async def get_jumpserver_audit_export(create_date: str, db: Session = Depends(get_db)):
    data = JumpserverAudit.get_data_by_create_date(db, create_date)
    return resp_200(data)
